package com.wgt.startx.util;


import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

/**
 * Created by wgt on 2017/7/30.
 */
public class CastUtil {

    public static int getInt(String numStr){
        if (StringUtils.isEmpty(numStr)) throw new NullPointerException();
        if (!NumberUtils.isDigits(numStr)) throw new NumberFormatException();
        return NumberUtils.toInt(numStr);
    }

    public static long getLong(String numStr){
        if (StringUtils.isEmpty(numStr)) throw new NullPointerException();
        if (!NumberUtils.isDigits(numStr)) throw new NullPointerException();
        return NumberUtils.toLong(numStr);
    }

    public static float getFloat(String numStr){
        if (StringUtils.isEmpty(numStr)) throw new NullPointerException();
        if (!NumberUtils.isNumber(numStr)) throw new NumberFormatException();
        return NumberUtils.toFloat(numStr);
    }

    public static double getDouble(String numStr){
        if (StringUtils.isEmpty(numStr)) throw new NullPointerException();
        if (!NumberUtils.isNumber(numStr)) throw new NumberFormatException();
        return NumberUtils.toDouble(numStr);
    }

}
